import { createStore } from 'vuex'
import axios from 'axios';
// 创建一个新的 store 实例



const store = createStore({
    state() {
        return {
            PluginStatus: false,
            ContentList: [],
            PluginList: [],
            mapping: {},
            ServicePath: EVENT_APP.getServicePath(),
        }
    },
    mutations: {
        ChangePluginDrawer(state, status) {
            state.PluginStatus = status;
        },
        appMappings(state) {
            for (let i = 0; i < state.PluginList.length; i++) {
                const element = state.PluginList[i];
                element.install = null;
                element.installing = false;
                if (!state.mapping[element.key]) {
                    state.mapping[element.key] = element;
                }
            };
            for (let i = 0; i < state.ContentList.length; i++) {
                const element = state.ContentList[i];
                const _ele = state.mapping[element.key]
                if (_ele) {
                    element.mapping.netVersion = _ele.lastVersion;
                    _ele.install = element.mapping.localVersion == _ele.lastVersion
                }
            };
            if (state.ContentList.length == 0 && state.PluginList.length != 0) {
                state.PluginStatus = true;
            };
        },
    },
    actions: {
        GetApps({ state, dispatch }) {
            state.ContentList = [];
            state.PluginList = [];
            state.mapping = {};
            EVENT_APP.getApps(res => {
                state.ContentList = res.map((item) => {
                    item.mapping = {
                        localVersion: item.localVersion,
                        progress: null,
                    }
                    return item;
                });
                dispatch('GetNetApps');
            });
        },
        GetNetApps({ state, commit }) {
            var newTime = new Date().getTime();
            axios.get(state.ServicePath + 'index.json?t=' + newTime).then(res => {
                console.log(res);
                state.PluginList = res.data.map((item) => {
                    item.install = null;
                    item.installing = false;
                    return item;
                });
                commit('appMappings');
            });
        },
        Install({ state, dispatch }, key) {
            var ContentItem = null;
            var PluginItem = null;
            for (let i = 0; i < state.ContentList.length; i++) {
                const element = state.ContentList[i];
                if (element.key == key) {
                    ContentItem = element;
                    break;
                }
            };
            for (let i = 0; i < state.PluginList.length; i++) {
                const element = state.PluginList[i];
                if (element.key == key) {
                    PluginItem = element;
                    break;
                }
            };
            if (ContentItem) {
                // 更新已有软件
                ContentItem.mapping.progress = 0;
                PluginItem.installing = true;
                dispatch('download', { ContentItem, PluginItem });
            } else {
                // 安装新软件
                ContentItem = {
                    icon: state.ServicePath + PluginItem.icon,
                    key: key,
                    path: key,
                    title: key,
                    name: PluginItem.title,
                    mapping: {
                        localVersion: PluginItem.lastVersion,
                        netVersion: PluginItem.lastVersion,
                        progress: 0,
                    }
                };
                state.ContentList.push(ContentItem);
                PluginItem.installing = true;
                ContentItem = state.ContentList[state.ContentList.length - 1]

                dispatch('download', { ContentItem, PluginItem });
            }
        },
        download({ state }, options) {
            const { ContentItem, PluginItem } = options;
            axios.get(state.ServicePath + PluginItem.path).then(res => {
                var FileName = res.data[0].FileName;
                var key = PluginItem.key;
                EVENT_APP.download(FileName, key, ContentItem.id, (type, value) => {
                    if (type == 'progress') {
                        ContentItem.mapping.progress = value;
                    } else if (type == 'success') {
                        ContentItem.mapping.progress = null;
                        ContentItem.mapping.localVersion = ContentItem.mapping.netVersion;
                        ContentItem.id = value.id;
                        ContentItem.index = value.index;
                        ContentItem.minHeight = value.minHeight;
                        ContentItem.minWidth = value.minWidth;
                        ContentItem.icon = value.icon;
                        PluginItem.install = true;
                        PluginItem.installing = false;
                    }
                });
            })
        },
        RunApp({ state }, options) {
            EVENT_APP.runApp(JSON.parse(JSON.stringify(options)));
        }
    },
})
export { store };